package com.company.common.auth.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.cache.CacheService;
import com.company.common.auth.AuthSecurityManager;
import com.company.common.auth.constant.AuthConstant;

public class AuthorizationUrlInterceptor extends HandlerInterceptorAdapter {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private CacheService cacheService;

    public AuthorizationUrlInterceptor(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String url = request.getServletPath();
        LoginUserInfo user = AuthSecurityManager.check(url, token, this.cacheService);
        if (user != null) {
            request.setAttribute(AuthConstant.REQ_USER_KEY, user);
        }
        return true;
    }


}
